Skip to content

Qwen3.5 on Metal holds its language model without the vision tower stored between its tensors: 0.61 GiB less on the 2B - #3407

Merged
ashhart merged 2 commits into
ashhart:mainfrom
jschmied:qwen35-metal-vision-span
Nov 19, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
jschmied:qwen35-metal-vision-span

Conversation

@jschmied

Copy link
Copy Markdown
Contributor

Branch qwen35-metal-vision-span @ e78aa5e, against main 2ba55ec. Family-only: families/qwen3_5.

Problem, measured

qwen3_5/model.zig loaded each shard with addFileSelected(..., "language_model."). That reads one contiguous span,
from the first selected tensor's start to the last one's end (core/checkpoint_metal.zig, the non-split path), and
keeps it on the GPU for the model's life. Qwen3.5 checkpoints store the vision tower inside that span.

On qwen35-2b (safetensors header):

  • language-model tensors: 0.987 GiB;
  • vision_tower: 0.617 GiB, of which 0.609 GiB lies inside the span.

The Metal Qwen3.5 server is text-only: it refuses image parts, and nothing in qwen3_5 reads the tower. So 26 % of
the 2B's GPU memory was held and never read. The Metal memory audit (MEMPROF-METAL) found it.

Change

  • addFileSplit(..., "language_model.", never): the same core path qwen27 (gpu_weights.repacked) and the Gemma 4
    vision loader (vision.zig's never) use. Every selected tensor packs, 256-byte aligned, into one buffer, and the
    file is closed. Same tensors, same bytes, new offsets; no tensor is deferred.
  • No core change, no kernel change. The other backends (CUDA, HIP) read their own way and are untouched.
  • Device test (model.zig): a shard laid out as a Qwen3.5 checkpoint (LM tensor, 4 KiB tower, LM tensor).
    • The old read holds 4,106 bytes; the new one holds 262 (the two tensors at 256-byte alignment) and no tower.
    • Both tensors' bytes equal the old read's.
    • It has a host-stub guard, and a planted wrong size turns it red.

Evidence (M3 Pro 36 GB, no tensor units; main = 2ba55ec built here)

Memory: footprint exact bytes, idle after load and after an 8K request

main GPU / phys this GPU / phys change
8K window 2.328 / 2.633 GiB 1.719 / 2.023 GiB -0.609 GiB
32K window 2.517 / 2.821 GiB 1.908 / 2.212 GiB -0.609 GiB
load peak phys (8K) 3.314 GiB 2.705 GiB -0.609 GiB
load line "2.3 GiB kept wired" "1.7 GiB kept wired"

Replies equal main (token_sha256, 64 tokens, thinking off):

  • greedy and seeded (T=1, seed 7) at about 8K and 32K prompt tokens;
  • a second turn resumed from the prompt cache (1,536 cached tokens) equals the same turn on a fresh server, on both builds;
  • concurrency: 8 prompts one at a time = the first 4 at --parallel 4 = all 8 at --parallel 8, on both builds;
  • every hash is the same on main and on this branch.

Image input: unchanged. Both builds refuse an image part with the same HTTP 400: "this server accepts text parts
only; image, audio and video inputs are unsupported". The refusal test is N/A, because nothing new is refused.

Speed, A-B-B-A, 3 rounds, a fresh server per run, 128 greedy tokens:

main this paired median pairs slower
TTFT 7,920 tokens 10.87/10.85/10.87/10.85/10.79/10.82 s 10.79/10.84/10.78/10.77/10.81/10.84 -0.37 % 2 of 6
decode at 8K 61.83/63.09/61.64/62.21/62.95/64.20 tok/s 62.47/64.77/63.28/64.60/62.57/63.19 +1.85 % 2 of 6
TTFT 26,952 tokens 68.28/68.28/68.23/68.40/68.50/68.40 s 68.19/68.33/68.35/68.21/69.56/68.35 0.00 % 3 of 6
decode at 27K 53.80/54.62/53.87/53.84/54.48/55.01 56.02/54.70/54.84/54.95/55.23/54.25 +1.59 % 1 of 6
load 2.0 s 2.0 s level (the 2 s readiness poll)

Each build's runs are in run order. One hash per prompt length in all 24 runs (f82ae54b64aa8302 at 8K,
ae57ce8c970f32cf at 27K). The 1-minute load average was 0.6-3.6 (logged per run; 01:58-02:16, the box otherwise idle).
No cell is slower by paired median.

  • The 27K TTFT cell has one slow run on this branch (69.56 s). Its paired median is 0.00 %, and the other five are
    within 68.19-68.35 s.
  • The decode gain is not claimed: the kernels and bytes are the same, only the buffer is smaller.

Checks:

  • zig build test exit 0: 865/865 steps, 14,052 tests passed (113 skipped).
  • lean_new: 0 new (341 on the base, 341 here).
  • zig fmt clean; files 600 lines or fewer.

Not run here: a tensor-unit Mac. The change is in the load path only, which is the same on every Mac.

Proposed CHANGELOG line

  • Qwen3.5 on Metal no longer keeps the vision tower its text-only server never reads in GPU memory: the 2B now holds
    1.7 GiB, down from 2.3. Replies are unchanged.

@ashhart

ashhart commented Nov 15, 2026

Copy link
Copy Markdown
Owner

DEVICE. @jschmied, the packing and ownership paths look sound. The 2B load keeps all 694 text tensors byte-identical and reduces actual Metal allocations by 654,442,496 bytes. All 32 text replies match across greedy, seeded, plain and concurrent drafted runs. Native and host checks pass with no new lean findings.

The device control is green. zig, zig-windows, pytest and fmt CI must also be green at this head before landing; this does not qualify other chips or model sizes.

  • Synthena (Ash's Agent)

@jschmied

Copy link
Copy Markdown
Contributor Author

@ashhart Thanks. CI is green at this head (e78aa5e): zig, zig-windows, pytest and fmt all pass. Agreed on scope: the measured saving is the 2B on one Mac; for the 35B MLX checkpoint we only have the header count (0.832 GiB of tower inside the old span), no device run.

@ashhart

ashhart commented Nov 15, 2026

Copy link
Copy Markdown
Owner

READY. @jschmied, CI is green at this head. The 2B check preserves all 694 text tensors and all 32 complete replies, with 654,442,496 fewer bytes of actual Metal allocation. The packing and ownership checks pass.

This is queued to land. The 35B header count remains a separate estimate, not a device result.

  • Synthena (Ash's Agent)

@ashhart

ashhart commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Thanks @jschmied. Ready to land at e78aa5ef413a. The 2B check preserves all 694 text tensors and all 32 complete replies, with 654,442,496 fewer bytes of Metal allocation. CI is green at this head. The 35B header estimate remains separate from device qualification.

  • Synthena

@ashhart
ashhart force-pushed the qwen35-metal-vision-span branch from e78aa5e to 42e459f Compare November 19, 2026 05:45
@ashhart
ashhart merged commit 5a61ce1 into ashhart:main Nov 19, 2026
2 of 11 checks passed
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thank you @jschmied. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants